data {
	// HTI dynamic model
	// Dynamic intercept for standards
	// Static intercept for events 
	 

	int N; 					// number of countries_years
	int prev_id[N];
	real deg_free[N];
	
	// Start with standards based
	int N_dest; // length of dest
	int y_dest[N_dest]; // vector of outcomes 
	int latent_dest_id[N_dest]; // vector that indexes latent variable 
	int year_dest_id[N_dest]; // vector that indexes year 
	
	int N_pdest; // length of pdest
	int y_pdest[N_pdest]; // vector of outcomes 
	int latent_pdest_id[N_pdest]; // vector that indexes latent variable 
	int year_pdest_id[N_pdest]; // vector that indexes year 
	
	int N_ldest; // length of oldest
	int y_ldest[N_ldest]; // vector of outcomes 
	int latent_ldest_id[N_ldest]; // vector that indexes latent variable 
	int year_ldest_id[N_ldest]; // vector that indexes year 
	
	int N_ddest; // length of ddest
	int y_ddest[N_ddest]; // vector of outcomes 
	int latent_ddest_id[N_ddest]; // vector that indexes latent variable
	int year_ddest_id[N_ddest]; // vector that indexes year 
	
	int N_dsdest; // length of dsdest
	int y_dsdest[N_dsdest]; // vector of outcomes 
	int latent_dsdest_id[N_dsdest]; // vector that indexes latent variable 
	int year_dsdest_id[N_dsdest]; // vector that indexes year 
	
	int N_cldest; // length of cldest
	int y_cldest[N_cldest]; // vector of outcomes 
	int latent_cldest_id[N_cldest]; // vector that indexes latent variable
	int year_cldest_id[N_cldest]; // vector that indexes year 
	
	int N_cpdest; // length of cpdest
	int y_cpdest[N_cpdest]; // vector of outcomes 
	int latent_cpdest_id[N_cpdest]; // vector that indexes latent variable
	int year_cpdest_id[N_cpdest]; // vector that indexes year 
	
	// Now go to event based (same but needs to be separated)
	int J_events_5; // number of events based items with 5 cat
	int N_events_5; // length of 5 cat events based vector 
	int y_events_5[N_events_5]; // vector of outcomes 
	int item_events_id_5[N_events_5]; // vector that indexes items 
	int latent_events_id_5[N_events_5]; // vector that indexes latent variable
	//int year_events_id_5[N_events_5]; // vector that indexes year 

}

transformed data {

	// Now to figure out how many years each of the standards based indicator has
	
	int min_year_dest = min(year_dest_id);
	int max_year_dest = max(year_dest_id);

	int min_year_pdest = min(year_pdest_id);
	int max_year_pdest = max(year_pdest_id);

	int min_year_ldest = min(year_ldest_id);
	int max_year_ldest = max(year_ldest_id);	 
	
	int min_year_ddest = min(year_ddest_id);
	int max_year_ddest = max(year_ddest_id);

	int min_year_dsdest = min(year_dsdest_id);
	int max_year_dsdest = max(year_dsdest_id);

	int min_year_cldest = min(year_cldest_id);
	int max_year_cldest = max(year_cldest_id);	
	
	int min_year_cpdest = min(year_cpdest_id);
	int max_year_cpdest = max(year_cpdest_id);
	
}

parameters {

	// latent variable parameters

	vector[N] theta_raw ; 		// matrix of raw thetas
	real<lower=0, upper=1> sigma; 	// innovation parameter

	//standards based with 2 indicators 
	real<lower=0> beta_dest; 
	ordered[2] cut_dest[max_year_dest - min_year_dest + 1];

	real<lower=0> beta_pdest; 
	ordered[2] cut_pdest[max_year_pdest - min_year_pdest + 1];
	
	real<lower=0> beta_ldest; 
	ordered[2] cut_ldest[max_year_ldest - min_year_ldest + 1];	
	
	real<lower=0> beta_ddest; 
	ordered[2] cut_ddest[max_year_ddest - min_year_ddest + 1];
	
	real<lower=0> beta_dsdest; 
	ordered[2] cut_dsdest[max_year_dsdest - min_year_dsdest + 1];
	
	real<lower=0> beta_cldest; 
	ordered[2] cut_cldest[max_year_cldest - min_year_cldest + 1];	
	
	real<lower=0> beta_cpdest; 
	ordered[2] cut_cpdest[max_year_cpdest - min_year_cpdest + 1];	

	//events with 5 cats
	real<lower=0> beta_events_5[J_events_5];
	ordered[5] cut_events_5[J_events_5];
	// ordered[5] cut_events_5;
	
}

transformed parameters {
	vector[N] theta; // actual theta

	// first year doesn't need to be adjusted
	for(ii in 1:N){
		if(prev_id[ii]==0){
			theta[ii] = theta_raw[ii];
		} else {
			{
			real tmp;
			tmp = theta[prev_id[ii]];
			theta[ii] =  tmp + theta_raw[ii] * sigma; 
			}
		}
	}

}

model {

	theta_raw ~ normal(0, 1);
	sigma ~ uniform(0, 1);  
	
	// *standards* below
 
	beta_dest ~ gamma(4,3);
	cut_dest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_dest - min_year_dest+1)){
		cut_dest[ii,] ~ normal(cut_dest[ii-1,], 4);
	}
		
	for(ii in 1:N_dest){
		y_dest[ii] ~ ordered_logistic((beta_dest) * theta[latent_dest_id[ii]], 
						to_vector(cut_dest[year_dest_id[ii] - min_year_dest + 1,] )) ;
	}

	beta_ldest ~ gamma(4,3);
	cut_ldest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_ldest - min_year_ldest+1)){
		cut_ldest[ii,] ~ normal(cut_ldest[ii-1,], 4);
	}
		
	for(ii in 1:N_ldest){
		y_ldest[ii] ~ ordered_logistic((beta_ldest) * theta[latent_ldest_id[ii]], 
						to_vector(cut_ldest[year_ldest_id[ii] - min_year_ldest + 1,] )) ;
	}
	
	beta_pdest ~ gamma(4,3);
	cut_pdest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_pdest - min_year_pdest+1)){
		cut_pdest[ii,] ~ normal(cut_pdest[ii-1,], 4);
	}
		
	for(ii in 1:N_pdest){
		y_pdest[ii] ~ ordered_logistic((beta_pdest) * theta[latent_pdest_id[ii]], 
						to_vector(cut_pdest[year_pdest_id[ii] - min_year_pdest + 1,] )) ;
	}

	beta_ddest ~ gamma(4,3);
	cut_ddest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_ddest - min_year_ddest+1)){
		cut_ddest[ii,] ~ normal(cut_ddest[ii-1,], 4);
	}
		
	for(ii in 1:N_ddest){
		y_ddest[ii] ~ ordered_logistic((beta_ddest) * theta[latent_ddest_id[ii]], 
						to_vector(cut_ddest[year_ddest_id[ii]  - min_year_ddest + 1,] )) ;
	}

	beta_dsdest ~ gamma(4,3);
	cut_dsdest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_dsdest - min_year_dsdest+1)){
		cut_dsdest[ii,] ~ normal(cut_dsdest[ii-1,], 4);
	}
		
	for(ii in 1:N_dsdest){
		y_dsdest[ii] ~ ordered_logistic((beta_dsdest) * theta[latent_dsdest_id[ii]], 
						to_vector(cut_dsdest[year_dsdest_id[ii]  - min_year_dsdest + 1,] )) ;
	}

	beta_cldest ~ gamma(4,3);
	cut_cldest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_cldest - min_year_cldest+1)){
		cut_cldest[ii,] ~ normal(cut_cldest[ii-1,], 4);
	}
		
	for(ii in 1:N_cldest){
		y_cldest[ii] ~ ordered_logistic((beta_cldest) * theta[latent_cldest_id[ii]], 
					to_vector(cut_cldest[year_cldest_id[ii]  - min_year_cldest + 1,] )) ;
	}

	beta_cpdest ~ gamma(4,3);
	cut_cpdest[1,] ~ normal(0,4);
	for(ii in 2:(max_year_cpdest - min_year_cpdest+1)){
		cut_cpdest[ii,] ~ normal(cut_cpdest[ii-1,], 4);
	}
		
	for(ii in 1:N_cpdest){
		y_cpdest[ii] ~ ordered_logistic((beta_cpdest) * theta[latent_cpdest_id[ii]], 
						to_vector(cut_cpdest[year_cpdest_id[ii]  - min_year_cpdest + 1,] )) ;
	}

	///////////////////////
	// Fixed cut points /// 
	///////////////////////
	 

	beta_events_5 ~ gamma(4,3);


	for(ii in 1:J_events_5){
		cut_events_5[ii,] ~ normal(0, 4);
	}
	
	for(ii in 1:N_events_5){
		y_events_5[ii] ~ ordered_logistic((beta_events_5[item_events_id_5[ii]]) * theta[latent_events_id_5[ii]], 
											cut_events_5[item_events_id_5[ii],]) ;
	}}

generated quantities {

	/// for log likelihood
	vector[N_dest] ll_dest;
	vector[N_ldest] ll_ldest;
	vector[N_pdest] ll_pdest;
	vector[N_ddest] ll_ddest;
	vector[N_dsdest] ll_dsdest; 
	vector[N_cldest] ll_cldest;
	vector[N_cpdest] ll_cpdest; 
	vector[N_events_5] ll_events_5; 

	/// for predictions 
	vector[N_dest] pred_dest;
	vector[N_ldest] pred_ldest;
	vector[N_pdest] pred_pdest;
	vector[N_ddest] pred_ddest;
	vector[N_dsdest] pred_dsdest; 
	vector[N_cldest] pred_cldest;
	vector[N_cpdest] pred_cpdest;
	vector[N_events_5] pred_events_5; 
		
	for(ii in 1:N_dest){
		ll_dest[ii] = ordered_logistic_lpmf(y_dest[ii] | (beta_dest) * theta[latent_dest_id[ii]], 
						to_vector(cut_dest[year_dest_id[ii] - min_year_dest + 1,] )) ;
	}
	
	
	for(ii in 1:N_ldest){
		ll_ldest[ii] = ordered_logistic_lpmf(y_ldest[ii] | (beta_ldest) * theta[latent_ldest_id[ii]], 
						to_vector(cut_ldest[year_ldest_id[ii] - min_year_ldest + 1,] )) ;
	}
	
		
	for(ii in 1:N_pdest){
		ll_pdest[ii] = ordered_logistic_lpmf(y_pdest[ii] | (beta_pdest) * theta[latent_pdest_id[ii]], 
						to_vector(cut_pdest[year_pdest_id[ii] - min_year_pdest + 1,] )) ;
	}

		
	for(ii in 1:N_ddest){
		ll_ddest[ii] = ordered_logistic_lpmf(y_ddest[ii] | (beta_ddest) * theta[latent_ddest_id[ii]], 
						to_vector(cut_ddest[year_ddest_id[ii] - min_year_ddest + 1,] )) ;
	}
	

	for(ii in 1:N_dsdest){
		ll_dsdest[ii] = ordered_logistic_lpmf(y_dsdest[ii] | (beta_dsdest) * theta[latent_dsdest_id[ii]], 
						to_vector(cut_dsdest[year_dsdest_id[ii] - min_year_dsdest + 1,] )) ;
	}
	
		
	for(ii in 1:N_cldest){
		ll_cldest[ii] = ordered_logistic_lpmf(y_cldest[ii] | (beta_cldest) * theta[latent_cldest_id[ii]], 
						to_vector(cut_cldest[year_cldest_id[ii] - min_year_cldest + 1,] )) ;
	}

	for(ii in 1:N_cpdest){
		ll_cpdest[ii] = ordered_logistic_lpmf(y_cpdest[ii] | (beta_cpdest) * theta[latent_cpdest_id[ii]], 
						to_vector(cut_cpdest[year_cpdest_id[ii] - min_year_cpdest + 1,] )) ;
	}
	
	for(ii in 1:N_events_5){
		ll_events_5[ii] = ordered_logistic_lpmf(y_events_5[ii] | (beta_events_5[item_events_id_5[ii]]) * theta[latent_events_id_5[ii]], 
											cut_events_5[item_events_id_5[ii],]) ;
	}

	////////////////////////////////
	/// Now generate predictions ///
	////////////////////////////////
 
	for(ii in 1:N_dest){
		pred_dest[ii] = ordered_logistic_rng((beta_dest) * theta[latent_dest_id[ii]], 
						to_vector(cut_dest[year_dest_id[ii] - min_year_dest + 1,] )) ;
	}
	
	
	for(ii in 1:N_ldest){
		pred_ldest[ii] = ordered_logistic_rng((beta_ldest) * theta[latent_ldest_id[ii]], 
						to_vector(cut_ldest[year_ldest_id[ii] - min_year_ldest + 1,] )) ;
	}
	
		
	for(ii in 1:N_pdest){
		pred_pdest[ii] = ordered_logistic_rng((beta_pdest) * theta[latent_pdest_id[ii]], 
						to_vector(cut_pdest[year_pdest_id[ii] - min_year_pdest + 1,] )) ;
	}

		
	for(ii in 1:N_ddest){
		pred_ddest[ii] = ordered_logistic_rng((beta_ddest) * theta[latent_ddest_id[ii]], 
						to_vector(cut_ddest[year_ddest_id[ii] - min_year_ddest + 1,] )) ;
	}
	

	for(ii in 1:N_dsdest){
		pred_dsdest[ii] = ordered_logistic_rng((beta_dsdest) * theta[latent_dsdest_id[ii]], 
						to_vector(cut_dsdest[year_dsdest_id[ii] - min_year_dsdest + 1,] )) ;
	}
	
		
	for(ii in 1:N_cldest){
		pred_cldest[ii] = ordered_logistic_rng((beta_cldest) * theta[latent_cldest_id[ii]], 
						to_vector(cut_cldest[year_cldest_id[ii] - min_year_cldest + 1,] )) ;
	}

	for(ii in 1:N_cpdest){
		pred_cpdest[ii] = ordered_logistic_rng((beta_cpdest) * theta[latent_cpdest_id[ii]], 
						to_vector(cut_cpdest[year_cpdest_id[ii] - min_year_cpdest + 1,] )) ;
	}
	
	for(ii in 1:N_events_5){
		pred_events_5[ii] = ordered_logistic_rng((beta_events_5[item_events_id_5[ii]]) * theta[latent_events_id_5[ii]], 
											cut_events_5[item_events_id_5[ii],]) ;
	}

}

